package cn.lianyutiantang.utils;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

import java.io.File;

/**doc转pdf工具类
 * Created by zhengjingli on 2017/3/3.
 */
public class Word2PdfUtil {
    static final int wdFormatPDF = 17;// PDF 格式

    //这个方法转换的时候不稳定，暂时废弃留下来以后可以做其他想法的参考
    public void wordToPDF(String sfileName,String toFileName){

        System.out.println("启动Word...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        Dispatch doc = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", new Variant(false));
            Dispatch docs = app.getProperty("Documents").toDispatch();
//          doc = Dispatch.call(docs,  "Open" , sourceFile).toDispatch();
            doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[] {
                    sfileName, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
            System.out.println("打开文档..." + sfileName);
            System.out.println("转换文档到PDF..." + toFileName);
            File tofile = new File(toFileName);

            if (tofile.exists()) {
                tofile.delete();
            }
//          Dispatch.call(doc, "SaveAs",  destFile,  17);
            Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
                    toFileName, new Variant(17) }, new int[1]);
            long end = System.currentTimeMillis();
            System.out.println("转换完成..用时：" + (end - start) + "ms.");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("========Error:文档转换失败：" + e.getMessage());
        } finally {
            Dispatch.call(doc,"Close");
            System.out.println("关闭文档");
            if (app != null)
                app.invoke("Quit", new Variant[] {});
        }
        //如果没有这句话,winword.exe进程将不会关闭
        ComThread.Release();
    }

    //马杰提供的方法
    public void wordToPDF1(String sourceFile,String toFilename){
        //String sourceFile = "e:\\word\\bbb.doc";
        int wdDoNotSaveChanges = 0;// 不保存待定的更改。
        int wdFormatPDF = 17;// PDF 格式

       // String toFilename = sourceFile.substring(0, sourceFile.length()-4) + ".pdf";
        System.out.println("启动Word...");
        long start = System.currentTimeMillis();
        ActiveXComponent app = null;
        try {
            app = new ActiveXComponent("Word.Application");
            app.setProperty("Visible", false);

            Dispatch docs = app.getProperty("Documents").toDispatch();
            System.out.println("打开文档..." + sourceFile);
            Dispatch doc = Dispatch.call(docs,//
                    "Open", //
                    sourceFile,// FileName
                    false,// ConfirmConversions
                    true // ReadOnly
            ).toDispatch();

            System.out.println("转换文档到PDF..." + toFilename);
            File tofile = new File(toFilename);
            if (tofile.exists()) {
                tofile.delete();
            }
            Dispatch.call(doc,//
                    "SaveAs", //
                    toFilename, // FileName
                    wdFormatPDF);

            Dispatch.call(doc, "Close", false);
            long end = System.currentTimeMillis();
            System.out.println("转换完成..用时：" + (end - start) + "ms.");
        } catch (Exception e) {
            System.out.println("========Error:文档转换失败：" + e.getMessage());
        } finally {
            if (app != null)
                app.invoke("Quit", wdDoNotSaveChanges);
        }

    }
    public static void main(String[] args) {
        Word2PdfUtil d = new Word2PdfUtil();
        //d.wordToPDF("D:\\360Downloads\\bbb.doc", "D:\\360Downloads\\fff.pdf");
       // d.wordToPDF("D:\\360Downloads\\ccc.doc", "D:\\360Downloads\\zmt.pdf");
       // d.wordToPDF("E:\\svnImgService\\source_code\\file\\target\\file\\file\\workReport\\patrol\\20170303\\96c519a4e8c940abbb82472a1dc74c83.doc", "E:\\svnImgService\\source_code\\file\\target\\file\\file\\workReport\\patrol\\20170303\\zmt.pdf");
       // d.wordToPDF("E:\\svnImgService\\source_code\\file\\target\\file\\file\\workReport\\patrol\\20170305\\3f6025e76ad046078d979f9220046368.doc", "E:\\svnImgService\\source_code\\file\\target\\file\\file\\workReport\\patrol\\20170305\\3f6025e76ad046078d979f9220046368.pdf");
       // d.wordToPDF1("E:\\svnImgService\\source_code\\file\\target\\file\\file\\workReport\\patrol\\20170305\\3f6025e76ad046078d979f9220046368.doc", "E:\\svnImgService\\source_code\\file\\target\\file\\file\\workReport\\patrol\\20170305\\3f6025e76ad046078d979f9220046368.pdf");
//        String ss="/file/workReport/patrol/20170305/11819903d06347b88a79e72a15e5f7fb.doc";
//        System.out.println(ss.substring(1));
        String unSplit = "E:\\apache-tomcat-7.0.67-file\\webapps\\file\\file\\workReport\\patrol\\20170305\\044104f5fa034436b61622b701df0874.doc";
        //根据长度取数组的最后一个元素
        int i=unSplit.lastIndexOf(".");
        System.out.println(unSplit.substring(0,i));
        System.out.println(unSplit.substring(0,i+1));
        System.out.println(unSplit.substring(0,i-1));
        System.out.println(unSplit.substring(1,i+1));
        System.out.println(unSplit.substring(1,i-1));
    }


}
